package study.java.jdk.jdk8._1_stream.sort;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public class sorted_List_Test {

    /**
     * 数字排序
     */
    @Test
    public void test1() {
        List<Integer> src = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        //升序
        List<Integer> ascLt = src.stream()
                .sorted()
                .collect(Collectors.toList());
        log.info("{}", ascLt);
        //倒序
        List<Integer> descLt1 = src.stream()
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList());
        log.info("{}", descLt1);
        List<Integer> descLt2 = src.stream()
                .sorted((x, y) -> y - x)
                .collect(Collectors.toList());
        log.info("{}", descLt2);
    }

    /**
     * 字符串排序
     */
    @Test
    public void test2() {
        List<String> src = Arrays.asList("a", "ba", "bb", "s2_switch", "cbb", "bba", "cab");
        //自然排序
        List<String> ascLt = src.stream()
                .sorted()
                .collect(Collectors.toList());
        log.info("{}", ascLt);

        //反转，倒序
        ascLt.sort(Collections.reverseOrder());

        //直接反转集合
        Collections.reverse(src);
    }
}
